package nl.rainbowsheep.reisorg.empl.site.controller;

import java.util.List;
import javax.validation.Valid;
import nl.rainbowsheep.reisorg.empl.site.model.Accommodation;
import nl.rainbowsheep.reisorg.empl.site.model.Country;
import nl.rainbowsheep.reisorg.empl.site.service.AccommodationService;
import nl.rainbowsheep.reisorg.empl.site.viewmodel.AccommodationViewmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller for Accommodation to delegate requests.
 */
@Controller
@RequestMapping(value = "/accommodation")
public class AccommodationController {

    @Autowired
    private AccommodationService accommodationService;

    @Autowired
    private CountryController countrycontroller;

    @RequestMapping(value = {"", "/", "/index"})
    public ModelAndView accommodationIndex() {
        ModelAndView modelAndView = this.getAllAccommodations();
        return modelAndView;
    }

    @RequestMapping(value = "/getall")
    public ModelAndView getAllAccommodations() {
        List<AccommodationViewmodel> accommodations = accommodationService.getAll();
        ModelAndView model = new ModelAndView("accommodation/index");
        model.addObject("accommodations", accommodations);
        return model;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView getAccommodationForm() {
        ModelAndView mav = new ModelAndView("accommodation/create");
        List<Country> countries = countrycontroller.getAllCountries();
        mav.addObject("accommodationViewmodel", new AccommodationViewmodel());
        mav.addObject("countries", countries);
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createAccommodationViewmodel(@ModelAttribute("accommodationViewmodel") @Valid AccommodationViewmodel accommodationViewmodel, BindingResult bindingResult, ModelAndView model) {
        if (bindingResult.hasErrors()) {
            return "create";
        }
        try {
            accommodationViewmodel.setDescription(accommodationViewmodel.getDescription_nl());
            accommodationService.insertAccommodationViewmodelNL(accommodationViewmodel);
            accommodationViewmodel.setDescription(accommodationViewmodel.getDescription_en());
            accommodationService.insertAccommodationViewmodelEN(accommodationViewmodel);
        } catch (Exception e) {
            e.toString();
        }
        return ("redirect:index");
    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ResponseBody
    public List<AccommodationViewmodel> findAccommodation(@RequestBody AccommodationViewmodel accommodationViewmodel) {
        List<AccommodationViewmodel> accommodations = accommodationService.getSearchResult(accommodationViewmodel);
        return accommodations;
    }
}
